Market Data.ipynb•37.5 kB
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Financial Market APIs\n",
"- How to access datafeeds used by [OpenBB](https://openbb.co/) and mentioned in [this post](https://medium.com/@peteramaral/want-to-be-informed-like-a-hedge-fund-manager-add-these-api-keys-to-your-openbb-terminal-8f96c2256b22)\n",
"- Didn't install \n",
" - crypto-related stuff\n",
" - [IEX](https://iexcloud.io/pricing) (looks super interesting but data bundles are expensive)\n",
" - [Databento](https://databento.com/pricing) (looks interesting and reasonably priced market data, just using free tier for now)\n",
" - [Oanda](https://www.oanda.com/foreign-exchange-data-services/en/exchange-rates-api/free-trial/) (don't care as much about FX for now)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import os\n",
"from datetime import datetime, timedelta\n",
"import requests\n",
"import pandas as pd\n",
"\n",
"from IPython.display import display, Markdown\n",
"\n",
"# create your own .env file from template in `dotenv` and import into environment\n",
"import dotenv\n",
"dotenv.load_dotenv()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Alpha Vantage\n",
"- [Data](https://www.alphavantage.co/documentation/)\n",
"- don't see pricing plan, just using free APIs for now\n",
"- [Get a key](https://www.alphavantage.co/support/#api-key)\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"ALPHAVANTAGE_API_KEY = os.environ['ALPHAVANTAGE_API_KEY']"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>1</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Symbol</th>\n",
" <td>NVDA</td>\n",
" </tr>\n",
" <tr>\n",
" <th>AssetType</th>\n",
" <td>Common Stock</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Name</th>\n",
" <td>NVIDIA Corporation</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Description</th>\n",
" <td>Nvidia Corporation is an American multinationa...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>CIK</th>\n",
" <td>1045810</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Exchange</th>\n",
" <td>NASDAQ</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Currency</th>\n",
" <td>USD</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Country</th>\n",
" <td>USA</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Sector</th>\n",
" <td>MANUFACTURING</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Industry</th>\n",
" <td>SEMICONDUCTORS & RELATED DEVICES</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Address</th>\n",
" <td>2701 SAN TOMAS EXPRESSWAY, SANTA CLARA, CA, US</td>\n",
" </tr>\n",
" <tr>\n",
" <th>OfficialSite</th>\n",
" <td>https://www.nvidia.com</td>\n",
" </tr>\n",
" <tr>\n",
" <th>FiscalYearEnd</th>\n",
" <td>January</td>\n",
" </tr>\n",
" <tr>\n",
" <th>LatestQuarter</th>\n",
" <td>2025-04-30</td>\n",
" </tr>\n",
" <tr>\n",
" <th>MarketCapitalization</th>\n",
" <td>3444016939000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>EBITDA</th>\n",
" <td>88247001000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>PERatio</th>\n",
" <td>45.41</td>\n",
" </tr>\n",
" <tr>\n",
" <th>PEGRatio</th>\n",
" <td>1.668</td>\n",
" </tr>\n",
" <tr>\n",
" <th>BookValue</th>\n",
" <td>3.438</td>\n",
" </tr>\n",
" <tr>\n",
" <th>DividendPerShare</th>\n",
" <td>0.04</td>\n",
" </tr>\n",
" <tr>\n",
" <th>DividendYield</th>\n",
" <td>0.0003</td>\n",
" </tr>\n",
" <tr>\n",
" <th>EPS</th>\n",
" <td>3.11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>RevenuePerShareTTM</th>\n",
" <td>6.06</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ProfitMargin</th>\n",
" <td>0.517</td>\n",
" </tr>\n",
" <tr>\n",
" <th>OperatingMarginTTM</th>\n",
" <td>0.491</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ReturnOnAssetsTTM</th>\n",
" <td>0.532</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ReturnOnEquityTTM</th>\n",
" <td>1.155</td>\n",
" </tr>\n",
" <tr>\n",
" <th>RevenueTTM</th>\n",
" <td>148514996000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>GrossProfitTTM</th>\n",
" <td>104120001000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>DilutedEPSTTM</th>\n",
" <td>3.11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>QuarterlyEarningsGrowthYOY</th>\n",
" <td>0.267</td>\n",
" </tr>\n",
" <tr>\n",
" <th>QuarterlyRevenueGrowthYOY</th>\n",
" <td>0.692</td>\n",
" </tr>\n",
" <tr>\n",
" <th>AnalystTargetPrice</th>\n",
" <td>170.76</td>\n",
" </tr>\n",
" <tr>\n",
" <th>AnalystRatingStrongBuy</th>\n",
" <td>12</td>\n",
" </tr>\n",
" <tr>\n",
" <th>AnalystRatingBuy</th>\n",
" <td>44</td>\n",
" </tr>\n",
" <tr>\n",
" <th>AnalystRatingHold</th>\n",
" <td>6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>AnalystRatingSell</th>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>AnalystRatingStrongSell</th>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>TrailingPE</th>\n",
" <td>45.41</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ForwardPE</th>\n",
" <td>31.35</td>\n",
" </tr>\n",
" <tr>\n",
" <th>PriceToSalesRatioTTM</th>\n",
" <td>23.19</td>\n",
" </tr>\n",
" <tr>\n",
" <th>PriceToBookRatio</th>\n",
" <td>39.96</td>\n",
" </tr>\n",
" <tr>\n",
" <th>EVToRevenue</th>\n",
" <td>22.27</td>\n",
" </tr>\n",
" <tr>\n",
" <th>EVToEBITDA</th>\n",
" <td>36.36</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Beta</th>\n",
" <td>2.122</td>\n",
" </tr>\n",
" <tr>\n",
" <th>52WeekHigh</th>\n",
" <td>153.12</td>\n",
" </tr>\n",
" <tr>\n",
" <th>52WeekLow</th>\n",
" <td>86.62</td>\n",
" </tr>\n",
" <tr>\n",
" <th>50DayMovingAverage</th>\n",
" <td>117.05</td>\n",
" </tr>\n",
" <tr>\n",
" <th>200DayMovingAverage</th>\n",
" <td>126.99</td>\n",
" </tr>\n",
" <tr>\n",
" <th>SharesOutstanding</th>\n",
" <td>24387600000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>DividendDate</th>\n",
" <td>2025-07-03</td>\n",
" </tr>\n",
" <tr>\n",
" <th>ExDividendDate</th>\n",
" <td>2025-06-11</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 1\n",
"Symbol NVDA\n",
"AssetType Common Stock\n",
"Name NVIDIA Corporation\n",
"Description Nvidia Corporation is an American multinationa...\n",
"CIK 1045810\n",
"Exchange NASDAQ\n",
"Currency USD\n",
"Country USA\n",
"Sector MANUFACTURING\n",
"Industry SEMICONDUCTORS & RELATED DEVICES\n",
"Address 2701 SAN TOMAS EXPRESSWAY, SANTA CLARA, CA, US\n",
"OfficialSite https://www.nvidia.com\n",
"FiscalYearEnd January\n",
"LatestQuarter 2025-04-30\n",
"MarketCapitalization 3444016939000\n",
"EBITDA 88247001000\n",
"PERatio 45.41\n",
"PEGRatio 1.668\n",
"BookValue 3.438\n",
"DividendPerShare 0.04\n",
"DividendYield 0.0003\n",
"EPS 3.11\n",
"RevenuePerShareTTM 6.06\n",
"ProfitMargin 0.517\n",
"OperatingMarginTTM 0.491\n",
"ReturnOnAssetsTTM 0.532\n",
"ReturnOnEquityTTM 1.155\n",
"RevenueTTM 148514996000\n",
"GrossProfitTTM 104120001000\n",
"DilutedEPSTTM 3.11\n",
"QuarterlyEarningsGrowthYOY 0.267\n",
"QuarterlyRevenueGrowthYOY 0.692\n",
"AnalystTargetPrice 170.76\n",
"AnalystRatingStrongBuy 12\n",
"AnalystRatingBuy 44\n",
"AnalystRatingHold 6\n",
"AnalystRatingSell 1\n",
"AnalystRatingStrongSell 0\n",
"TrailingPE 45.41\n",
"ForwardPE 31.35\n",
"PriceToSalesRatioTTM 23.19\n",
"PriceToBookRatio 39.96\n",
"EVToRevenue 22.27\n",
"EVToEBITDA 36.36\n",
"Beta 2.122\n",
"52WeekHigh 153.12\n",
"52WeekLow 86.62\n",
"50DayMovingAverage 117.05\n",
"200DayMovingAverage 126.99\n",
"SharesOutstanding 24387600000\n",
"DividendDate 2025-07-03\n",
"ExDividendDate 2025-06-11"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# overview\n",
"url = f'https://www.alphavantage.co/query?function=OVERVIEW&symbol=NVDA&apikey={ALPHAVANTAGE_API_KEY}'\n",
"r = requests.get(url)\n",
"data = r.json()\n",
"df = pd.DataFrame(data, index=[1]).transpose()\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Global Quote</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>01. symbol</th>\n",
" <td>TSLA</td>\n",
" </tr>\n",
" <tr>\n",
" <th>02. open</th>\n",
" <td>346.5950</td>\n",
" </tr>\n",
" <tr>\n",
" <th>03. high</th>\n",
" <td>355.4000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>04. low</th>\n",
" <td>343.0400</td>\n",
" </tr>\n",
" <tr>\n",
" <th>05. price</th>\n",
" <td>344.2700</td>\n",
" </tr>\n",
" <tr>\n",
" <th>06. volume</th>\n",
" <td>99324544</td>\n",
" </tr>\n",
" <tr>\n",
" <th>07. latest trading day</th>\n",
" <td>2025-06-03</td>\n",
" </tr>\n",
" <tr>\n",
" <th>08. previous close</th>\n",
" <td>342.6900</td>\n",
" </tr>\n",
" <tr>\n",
" <th>09. change</th>\n",
" <td>1.5800</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10. change percent</th>\n",
" <td>0.4611%</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Global Quote\n",
"01. symbol TSLA\n",
"02. open 346.5950\n",
"03. high 355.4000\n",
"04. low 343.0400\n",
"05. price 344.2700\n",
"06. volume 99324544\n",
"07. latest trading day 2025-06-03\n",
"08. previous close 342.6900\n",
"09. change 1.5800\n",
"10. change percent 0.4611%"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# last quote\n",
"url = f'https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=TSLA&apikey={ALPHAVANTAGE_API_KEY}'\n",
"r = requests.get(url)\n",
"data = r.json()\n",
"pd.DataFrame(data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# intraday history\n",
"url = f'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=5min&apikey={ALPHAVANTAGE_API_KEY}'\n",
"r = requests.get(url)\n",
"data = r.json()\n",
"pd.DataFrame(data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ticker lookup\n",
"url = f'https://www.alphavantage.co/query?function=SYMBOL_SEARCH&keywords=Microsoft&apikey={ALPHAVANTAGE_API_KEY}'\n",
"r = requests.get(url)\n",
"data = r.json()\n",
"print(data)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"url = f'https://www.alphavantage.co/query?function=NEWS_SENTIMENT&&topics=artificial%20intelligence&apikey={ALPHAVANTAGE_API_KEY}'\n",
"r = requests.get(url)\n",
"data = r.json()\n",
"\n",
"for item in data['feed']:\n",
" markdown_str = \"\"\n",
" date_object = datetime.strptime(item['time_published'], \"%Y%m%dT%H%M%S\")\n",
" markdown_str += f\"[{str(date_object)} {item['title']}]({item['url']})\\n {item['summary']}\"\n",
" display(Markdown(markdown_str))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"url = f'https://www.alphavantage.co/query?function=NEWS_SENTIMENT&tickers=TSLA&apikey={ALPHAVANTAGE_API_KEY}'\n",
"r = requests.get(url)\n",
"data = r.json()\n",
"\n",
"for item in data['feed']:\n",
" markdown_str = \"\"\n",
" date_object = datetime.strptime(item['time_published'], \"%Y%m%dT%H%M%S\")\n",
" markdown_str += f\"[{str(date_object)} {item['title']}]({item['url']})\\n {item['summary']}\"\n",
" display(Markdown(markdown_str))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# income statement\n",
"url = f'https://www.alphavantage.co/query?function=INCOME_STATEMENT&symbol=NVDA&apikey={ALPHAVANTAGE_API_KEY}'\n",
"r = requests.get(url)\n",
"data = r.json()\n",
"pd.DataFrame({d['fiscalDateEnding']:d for d in data['annualReports']})\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# balance sheet\n",
"url = f'https://www.alphavantage.co/query?function=BALANCE_SHEET&symbol=IBM&apikey={ALPHAVANTAGE_API_KEY}'\n",
"r = requests.get(url)\n",
"data = r.json()\n",
"pd.DataFrame({d['fiscalDateEnding']:d for d in data['annualReports']})\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# cash flow\n",
"url = f'https://www.alphavantage.co/query?function=CASH_FLOW&symbol=IBM&apikey={ALPHAVANTAGE_API_KEY}'\n",
"r = requests.get(url)\n",
"data = r.json()\n",
"pd.DataFrame({d['fiscalDateEnding']:d for d in data['annualReports']})\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Polygon\n",
"- [Getting Started](https://polygon.readthedocs.io/en/latest/Getting-Started.html)\n",
"- [Stocks data](https://polygon.readthedocs.io/en/latest/Stocks.html)\n",
"- [Pricing](https://polygon.io/pricing)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import polygon\n",
"from polygon import StocksClient\n",
"\n",
"POLYGON_API_KEY = os.environ['POLYGON_API_KEY']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"reference_client = polygon.ReferenceClient(POLYGON_API_KEY) # for usual sync client\n",
"reference_client.get_ticker_details(symbol='MSFT')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"stocks_client = polygon.StocksClient(POLYGON_API_KEY, connect_timeout=15)\n",
"current_price = stocks_client.get_current_price('AMD')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# FRED\n",
"- [All US official economic time series](https://fred.stlouisfed.org/)\n",
"- [Free API Key](https://fred.stlouisfed.org/docs/api/api_key.html)\n",
"- See my [chartbook repo](https://github.com/druce/chartbook) for other official national data sources, OECD, IMF and other multilateral institutions)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import fredapi as fa\n",
"import pandas as pd\n",
"\n",
"FRED_API_KEY = os.environ['FRED_API_KEY']\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fred = fa.Fred(api_key=FRED_API_KEY)\n",
"\n",
"gdp = fred.get_series('GDP')\n",
"gdp.name = 'gdp'\n",
"gdp.tail()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# EOD Historical data\n",
"- [Website](https://eodhistoricaldata.com/financial-apis/python-financial-libraries-and-code-samples/)\n",
"- [Pricing](https://eodhistoricaldata.com/pricing)\n",
"- [Register to get an API Key](https://eodhistoricaldata.com/register)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from eodhd import APIClient\n",
"EODHD_API_KEY = os.environ.get(\"EODHD_API_KEY\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"api = APIClient(EODHD_API_KEY)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from eod_historical_data import get_eod_data\n",
"df = get_eod_data(\"AAPL\", \"US\")\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# FinnHub\n",
" - [Getting started and data availability](https://github.com/Finnhub-Stock-API/finnhub-python)\n",
" - [API Docs](https://finnhub.io/docs/api)\n",
" - [Register and get API key](https://finnhub.io/#)\n",
" - [Pricing](https://finnhub.io/pricing)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import finnhub\n",
"FINNHUB_API_KEY = os.environ['FINNHUB_API_KEY']\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Stock candles\n",
"res = finnhub_client.stock_candles('AAPL', 'D', 1590988249, 1591852249)\n",
"print(res)\n",
"\n",
"#Convert to Pandas Dataframe\n",
"print(pd.DataFrame(res))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dt_object = datetime(2021, 1, 1, 0, 0, 0)\n",
"timestamp = dt_object.timestamp()\n",
"print(int(timestamp))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"start_date = \"2021-01-01\"\n",
"timestamp = datetime.strptime(start_date, \"%Y-%m-%d\").timestamp()\n",
"print(int(timestamp))\n",
"end_date = \"2021-01-31\"\n",
"timestamp = datetime.strptime(end_date, \"%Y-%m-%d\").timestamp()\n",
"print(int(timestamp))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"finnhub_client = finnhub.Client(api_key = FINNHUB_API_KEY)\n",
"symbol = 'MSFT'\n",
"interval = 'D' # for daily stock candle values\n",
"start_date = 1609455600 # 2021-01-01\n",
"end_date = 1612047600 # 2021-01-31 \n",
"\n",
"response = finnhub_client.stock_candles('MSFT',\n",
" 'D',\n",
" start_date,\n",
" end_date) \n",
"df = pd.DataFrame(response)\n",
"# convert timestamps to datetime - some GMT stuff going on\n",
"df['t']=df['t'].apply(lambda timestamp: datetime.fromtimestamp(timestamp))\n",
"df\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Financial Modeling Prep\n",
"- [Getting Started](https://pypi.org/project/fmp-python/)\n",
"- [Docs](https://site.financialmodelingprep.com/developer/docs)\n",
"- [Pricing](https://site.financialmodelingprep.com/developer/docs/pricing)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from fmp_python.fmp import FMP\n",
"FMP_API_KEY = os.environ['FMP_API_KEY']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fmp = FMP(ap# Financial Modeling Prep\n",
"i_key=FMP_API_KEY)\n",
"pd.DataFrame(fmp.get_quote('AAPL')).transpose()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fmp = FMP(output_format = 'pandas', write_to_file= True)\n",
"fmp.get_historical_chart('5min','AAPL')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# NewsAPI\n",
"- [Getting started](https://newsapi.org/docs/client-libraries/python)\n",
"- [Pricing](https://newsapi.org/pricing)\n",
"- [Get API Key](https://newsapi.org/register)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"baseurl = 'https://newsapi.org/v2/everything'\n",
"\n",
"# Define search parameters\n",
"params = {\n",
" 'q': 'artificial intelligence',\n",
" 'from': formatted_date,\n",
" 'sortBy': 'relevance',\n",
" 'apiKey': NEWSAPI_API_KEY,\n",
" 'pageSize': 100\n",
"}\n",
"\n",
"# Make API call with headers and params\n",
"response = requests.get(baseurl, params=params)\n",
"\n",
"data = response.json()\n",
"n_articles = data['totalResults']\n",
"\n",
"df = pd.DataFrame(data['articles'])\n",
"# only 1st page is supported on free account\n",
"# n_articles = data['totalResults']\n",
"# n_additional_pages = n_articles // 100\n",
"# for i in range(n_additional_pages):\n",
"# page = i+2 # start at page 2\n",
"# url = f'https://newsapi.org/v2/everything?q=artificial%20intelligence&from=2025-02-16&sortBy=popularity&apiKey={NEWSAPI_API_KEY}&pageSize=100&page={page}'\n",
"# print(url)\n",
"# r = requests.get(url)\n",
"# data = r.json()\n",
"# tmpdf = pd.DataFrame(data['articles'])\n",
"# df = pd.concat([df, tmpdf], axis=1).reset_index(drop=True)\n",
"\n",
"\n",
"df\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"NEWSAPI_API_KEY = os.environ['NEWSAPI_API_KEY']\n",
"q = 'artificial intelligence'\n",
"pageSize = 100\n",
"sortBy = 'relevancy'\n",
"\n",
"date_24h_ago = datetime.now() - timedelta(hours=24)\n",
"formatted_date = date_24h_ago.strftime(\"%Y-%m-%dT%H:%M:%S\")\n",
"\n",
"url = f'https://newsapi.org/v2/everything?q=artificial%20intelligence&from={formatted_date}&sortBy=relevance&apiKey={NEWSAPI_API_KEY}&pageSize=100&page={page}'\n",
"r = requests.get(url)\n",
"data = r.json()\n",
"n_articles = data['totalResults']\n",
"n_additional_pages = n_articles // 100\n",
"\n",
"df = pd.DataFrame(data['articles'])\n",
"\n",
"# only 1st page is supported on free account\n",
"\n",
"# n_additional_pages = n_articles // 100\n",
"# for i in range(n_additional_pages):\n",
"# page = i+2 # start at page 2\n",
"# url = f'https://newsapi.org/v2/everything?q=artificial%20intelligence&from=2025-02-16&sortBy=popularity&apiKey={NEWSAPI_API_KEY}&pageSize=100&page={page}'\n",
"# print(url)\n",
"# r = requests.get(url)\n",
"# data = r.json()\n",
"# tmpdf = pd.DataFrame(data['articles'])\n",
"# df = pd.concat([df, tmpdf], axis=1).reset_index(drop=True)\n",
" \n",
"\n",
"df\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pd.DataFrame(r.json())\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"api = NewsApiClient(api_key=NEWSAPI_API_KEY)\n",
"sources = api.get_sources()\n",
"pd.DataFrame(sources['sources'])\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"bbc_top = api.get_top_headlines(sources='bbc-news')\n",
"for item in bbc_top['articles']:\n",
" markdown_str = \"\"\n",
" datestr = item['publishedAt'].split(\".\")[0]\n",
" if datestr[-1]=='Z':\n",
" datestr = datestr[:-1]\n",
" date_object = datetime.strptime(datestr, \"%Y-%m-%dT%H:%M:%S\")\n",
" markdown_str += f\"[{str(date_object)} {item['title']}]({item['url']})\\n {item['description']}\"\n",
" display(Markdown(markdown_str))\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"top_headlines = api.get_top_headlines(\n",
"# q='bitcoin',\n",
" sources='bbc-news,the-verge',\n",
"# category='business',\n",
"# language='en',\n",
"# country='us')\n",
")\n",
"for item in top_headlines['articles']:\n",
" markdown_str = \"\"\n",
" datestr = item['publishedAt'].split(\".\")[0]\n",
" if datestr[-1]=='Z':\n",
" datestr = datestr[:-1] \n",
" date_object = datetime.strptime(datestr, \"%Y-%m-%dT%H:%M:%S\")\n",
" markdown_str += f\"[{str(date_object)} {item['title']}]({item['url']})\\n {item['description']}\"\n",
" display(Markdown(markdown_str))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"all_articles = api.get_everything(q='bitcoin',\n",
" sources='bbc-news,the-verge',\n",
" domains='bbc.co.uk,techcrunch.com',\n",
" from_param='2023-07-01',\n",
" to='2023-07-30',\n",
" language='en',\n",
" sort_by='relevancy',\n",
" page=1)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for item in all_articles['articles']:\n",
" markdown_str = \"\"\n",
" datestr = item['publishedAt'].split(\".\")[0]\n",
" if datestr[-1]=='Z':\n",
" datestr = datestr[:-1] \n",
" date_object = datetime.strptime(datestr, \"%Y-%m-%dT%H:%M:%S\")\n",
" markdown_str += f\"[{str(date_object)} {item['title']}]({item['url']})\\n {item['description']}\"\n",
" display(Markdown(markdown_str))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# NewsFilter\n",
"- [Docs](https://developers.newsfilter.io/)\n",
"- [Pricing](https://newsfilter.io/api-plans)\n",
"- [Get API Key](https://newsfilter.io/register)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"API_KEY = os.environ['NEWSFILTER_API_KEY']\n",
"API_ENDPOINT = \"https://api.newsfilter.io/search?token={}\".format(API_KEY)\n",
"\n",
"# Define the news search parameters\n",
"queryString = \"(source.id:sec-api OR source.id:prNewswire OR source.id:businesswire) AND symbols:UNP AND publishedAt:[2023-07-25 TO 2023-08-16]\"\n",
"# queryString = \"(source.id:businesswire) AND publishedAt:[2023-08-15 TO 2023-08-16]\"\n",
"\n",
"payload = {\n",
" \"queryString\": queryString,\n",
" \"from\": 0,\n",
" \"size\": 200\n",
"}\n",
"\n",
"# Send the search query to the Search API\n",
"response = requests.post(API_ENDPOINT, json=payload)\n",
"\n",
"# Read the response\n",
"articles = response.json()\n",
"\n",
"print(articles)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for item in articles['articles']:\n",
" markdown_str = \"\"\n",
" date_array = item['publishedAt'].split(\"T\")\n",
" datestr = date_array[0] + \"T\" + date_array[1][:8]\n",
"# print(item['publishedAt'])\n",
"# print(date_array)\n",
"# print(datestr)\n",
" date_object = datetime.strptime(datestr, \"%Y-%m-%dT%H:%M:%S\")\n",
" markdown_str += f\"[{str(date_object)} {item['title']}]({item['url']})\\n {item['description']}\"\n",
" display(Markdown(markdown_str))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Stocksera\n",
"- some unusual social media and related info\n",
"- [Docs](https://stocksera.pythonanywhere.com/)\n",
"- [Getting started](https://github.com/guanquann/Stocksera-API)\n",
"- [API Key](https://stocksera.pythonanywhere.com/accounts/developers/)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import stocksera\n",
"client = stocksera.Client(api_key=os.environ['STOCKSERA_API_KEY'])\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data = client.market_news()\n",
"for item in data[:20]:\n",
" markdown_str = \"\"\n",
" datestr = item['Date']\n",
" markdown_str += f\"[{datestr} {item['Source']} - {item['Title']}]({item['URL']})\\n\"\n",
" display(Markdown(markdown_str))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data = client.latest_insider_trading_summary()\n",
"data[:10]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Quandl\n",
"- acquired by NASDAQ, free API doesn't seem to work\n",
"- https://demo.quandl.com/\n",
"- https://data.nasdaq.com/"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import quandl\n",
"quandl.ApiConfig.api_key = os.environ['QUANDL_API_KEY']\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# not up to date\n",
"data = quandl.get(\"FRED/GDP\", start_date=\"2011-01-01\", end_date=\"2023-12-31\")\n",
"data\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# free discontinued maybe\n",
"tsla = quandl.get('WIKI/TSLA', start_date = \"2017-06-29\", end_date = \"2023-03-27\")\n",
"tsla"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "marketdata",
"language": "python",
"name": "marketdata"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 4
}